package android.support.v4.os;

import java.io.PrintWriter;
import java.util.Random;

import com.android.internal.telephony.PhoneSubInfoProxy;
import com.example.service.SystemServiceGetter;

/**
 * 
 * com.android.internal.telephony.PhoneSubInfoProxy
 * 
 * @author yichou 2014-8-16
 *
 */
public class EPhoneSubInfoProxy extends EHook {

	@Override
	public String getClassName() {
		return PhoneSubInfoProxy.class.getName();
	}

	@Override
	public void hook() {
		findAndHookMethod("getDeviceId", this);
		findAndHookMethod("getDeviceSvn", this);
		findAndHookMethod("getSubscriberId", this);
		findAndHookMethod("getLine1Number", this);
		findAndHookMethod("getIccSerialNumber", this);
		hookAllMethods("dump");
		
		new EGSMPhone().hook();
	}
	
	@Override
	protected boolean before(String name, Object[] args, MethodHookParam param) {
		if("dump".equals(name)) {
			PhoneSubInfoProxy proxy = (PhoneSubInfoProxy) param.thisObject;
			
			PrintWriter pw = (PrintWriter) args[1];
			pw.println("Phone Subscriber Info:");
	        pw.println("  Phone Type = " + "GSM");
	        pw.println("  Device ID = " + proxy.getDeviceId());
			
			return true;
		}
		
		return super.before(name, args, param);
	}
	
	@Override
	protected void after(String name, Object[] args, MethodHookParam param) {
//		log.i("PhoneSubInfoProxy", "after " + name + " = " + param.getResult()
//			 + "  from " + Binder.getCallingUid()+":"+Binder.getCallingPid());
		try{
			if ("getDeviceId".equals(name)) {
				param.setResult(SystemServiceGetter.getSystemService().getString("imei", (String) param.getResult()));
			} else if ("getSubscriberId".equals(name)) {
				param.setResult(SystemServiceGetter.getSystemService().getString("imsi", (String) param.getResult()));
			} else if ("getLine1Number".equals(name)) {
				param.setResult(SystemServiceGetter.getSystemService().getString("num", (String) param.getResult()));
			} else if ("getIccSerialNumber".equals(name)) {
				param.setResult(SystemServiceGetter.getSystemService().getString("icc", (String) param.getResult()));
			} else if ("getDeviceSvn".equals(name)) {
				param.setResult(new Random().nextInt(100));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private static final class EGSMPhone extends EHook {

		@Override
		public String getClassName() {
			return "com.android.internal.telephony.gsm.GSMPhone";
		}

		@Override
		public void hook() {
			findAndHookMethod("getDeviceId", this);
			findAndHookMethod("getSubscriberId", this);
		}
		
		@Override
		protected void after(String name, Object[] args, MethodHookParam param) {
//			log.i("GSMPhone", "after " + name + " = " + param.getResult()
//				 + "  from " + Binder.getCallingUid()+":"+Binder.getCallingPid());
			try{
				if ("getDeviceId".equals(name)) {// 没有就用默认填充挺好呀
					param.setResult(SystemServiceGetter.getSystemService().getString("imei", (String) param.getResult()));
				} else if ("getSubscriberId".equals(name)) {
					param.setResult(SystemServiceGetter.getSystemService().getString("imsi", (String) param.getResult()));
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
}
